package pl.prv.polanie.client.dialogs;

import pl.prv.polanie.client.Planner;
import pl.prv.polanie.client.dto.CSSubgroupTypeDTO;
import pl.prv.polanie.client.model.Cache;
import pl.prv.polanie.client.model.ServiceHolder;
import pl.prv.polanie.client.panels.lists.SubgroupTypesList;
import pl.prv.polanie.client.widgets.CustomDialogBox;
import pl.prv.polanie.client.widgets.TextBoxStyled;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

/**
 * 
 * @author Janusz Prokulewicz
 * Created: Apr 19, 2008
 */

public class CreateSubgroupTypeDialog extends CustomDialogBox {

//	private static final String STYLE__CREATE_SUBJECT_EVENT_DIALOG = "createSubjectEventDialog";
	
	
	/** Podzial, dla ktorego jest tworzony nowy/edytowany istniejacy subjectEvent.*/
	private SubgroupTypesList subgroupTypesList = null;
	
	
	
	
	/***/
	private FlexTable widgetsFT = new FlexTable();
	
	private Label subgroupTypeNameL= new Label("Name");
	private TextBox subgroupTypeNameTB = new TextBoxStyled();
	

//	private Button loadDataB = new Button("load dataa");

	private Button saveOrCreateAndCloseB = new Button("save and close");
	private Button saveOrCreateB = new Button("save/create");
	private Button closeB = new Button("close");
	
	
	//referencja do dto edytowanego at
	private CSSubgroupTypeDTO csSubgroupTypeDTO = null;
	
	
	public CreateSubgroupTypeDialog(SubgroupTypesList subjectTypesList, CSSubgroupTypeDTO csST) {
		this.subgroupTypesList = subjectTypesList;
		this.csSubgroupTypeDTO = csST;
		
		layoutWidgets();
		setupWidgets();

		setWidget(widgetsFT);
//		setStyleName(STYLE__CREATE_SUBJECT_EVENT_DIALOG);
		if (csST != null) {
			setText("Edit subgroup type");
		} else {
			setText("Create new subgroup type");
		}
		
		
		loadDataToWidgets();
	}



	private void setupWidgets() {
		
		subgroupTypeNameTB.setText("");
		
		saveOrCreateB.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				boolean closeWindowAfterSaving = false;
				saveCSSubgroupType(closeWindowAfterSaving);
			}
		});
		
		saveOrCreateAndCloseB.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				boolean closeWindowAfterSaving = true;
				saveCSSubgroupType(closeWindowAfterSaving);
			}
		});
		
		closeB.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				hide();
			}
		});
		
		
//		loadDataB.addClickListener(new ClickListener() {
//			public void onClick(Widget sender) {
//				loadDataToWidgets();
//			}
//		});
	}



	private void layoutWidgets() {
		widgetsFT.setWidget(0, 0, subgroupTypeNameL);
		widgetsFT.setWidget(0, 1, subgroupTypeNameTB);

//		widgetsFT.setWidget(1, 0, loadDataB);
		widgetsFT.setWidget(1, 1, saveOrCreateAndCloseB);
		
		widgetsFT.setWidget(2, 0, saveOrCreateB);
		widgetsFT.setWidget(2, 1, closeB);
	}
	
	/**
	 * TODO metoda validujaca dane
	 */
	private boolean validateEnteredData() {
		return true;
	}
	

	
	/**
	 * Wczytuje z cache'a dane do kontrolek. W przypadku edycji
	 * istniejacego SE ustawiane sa takze w kontrolkach odpowiednie wartosci.
	 */
	private void loadDataToWidgets() {
		String name = "";
		
		if (csSubgroupTypeDTO != null) {
			
			Planner.log("edycja subject type");
			name = csSubgroupTypeDTO.getName();
			
		} else {
			Planner.log("tworzenie subject type");
		}

		subgroupTypeNameTB.setText("" + name);
	}

	/**
	 * Zapisuje/aktualizuje rekord. 
	 * @param closeWindowAfterSaving
	 */
	private void saveCSSubgroupType(final boolean closeWindowAfterSaving) {
		if (validateEnteredData()) {
			final CSSubgroupTypeDTO csST = new CSSubgroupTypeDTO();
			
			//w przypadku edycji AT ustawiamy id
			if (csSubgroupTypeDTO != null) {
				csST.setId(csSubgroupTypeDTO.getId());
			}
			
			
			//ustawienie atrybutow w dto
			csST.setName(subgroupTypeNameTB.getText());
			
			
			AsyncCallback<Long> callback = new AsyncCallback<Long>() {
				public void onFailure(Throwable caught) {
					Planner.log("CreateSubgroupTypeDialog.saveCSSubgroupType() FAILURE");
				}
				public void onSuccess(Long res) {
					Planner.log("CreateSubgroupTypeDialog.saveCSSubgroupType() SUCCESS -> zwrocone ID = " + res);

					
					//update w cache
					csST.setId(res.longValue());
					Cache.saveOrUpdateCSSubgroupType(csST);

					
					//TODO dodac do kontrolki tabelki
					if (subgroupTypesList != null) {
						subgroupTypesList.addCSSubgroupType(csST);
					}

					
					if (closeWindowAfterSaving) {
						hide();
					}
				}
			};
			
			ServiceHolder.get().saveCSSubgroupType(csST, callback);
		}
	}


}
